/**
 ** element — DOM operations
 * 
 * The element module contains useful functions for working with the DOM.
 *
 */

define(
'oj/element',
[
],
function () {
    'use strict';
    
    
    var element = {
        'create': create,
        'absoluteTop': absoluteTop,
        'absoluteLeft': absoluteLeft
    };
    
    
    
    function create(parent, tag, classes, attributes, content) {
        /**
         *>element.create(parent, 'tag', 'classes', {attributes}, [content])
         * 
         * Creates a HTMLElement instance.
         * 
         * Parent is an optional HTMLElement to attach the new HTMLElement
         * named tag. A class and attributes will be assigned if specifiec,
         * with a list of HTMLElements and strings appended as content.
         * 
         *. var container = element.create(document.body, 'div', 'container', [
         *.   element.create('p', ['A paragraph inside a div'])
         *. ]);
         *
         */
        
        if (Object.type(parent) === 'String') {
            content = attributes;
            attributes = classes;
            classes = tag;
            tag = parent;
        }
        
        if (Object.type(tag) !== 'String') {
            throw 'Expected a string for name but received ' + Object.type(tag);
        }
        
        var el = document.createElement(tag);
        
        if (Object.type(classes) === 'String') {
            el.setAttribute('class', classes);
        } else {
            content = attributes;
            attributes = classes;
        }

        if (Object.type(attributes) === 'Object') {
            Object.keys(attributes).forEach(function (attrib) {
                el.setAttribute(attrib, attributes[attrib]);
            });
        } else {
            content = attributes;
        }

        if (Object.type(content) === 'Array') {
            tag = -1;
            while (tag++ < content.length) {
                if (content[tag] instanceof HTMLElement) {
                    el.appendChild(content[tag]);
                } else if (Object.type(content[tag]) === 'String') {
                    el.innerHTML += content[tag] + '\n';
                }
            }
        }

        if (parent instanceof HTMLElement) {
            parent.appendChild(el);
        }
        
        return el;
        
    }
    
    
    
    function absoluteTop(el) {
        /**
         *>element.absoluteTop(el)
         * 
         * Returns the absolute top position of the element.
         * 
         */
        
        var top = 0;
        
        while (el != null) {
            top += el.offsetTop;
            el = el.offsetParent;
        }
        
        return top;
        
    }
    
    
    
    function absoluteLeft(el) {
        /**
         *>element.absoluteLeft(el)
         * 
         * Returns the absolute left position of the element.
         * 
         */
        
        var left = 0;
        
        while (el != null) {
            left += el.offsetLeft;
            el = el.offsetParent;
        }
        
        return left;
        
    }
    
    
    return element;

});
